import java.util.ArrayList;
import java.util.List;

public class Solution729 {

    List<int[]> list=new ArrayList<>();

    public Solution729() {

    }

    public boolean book(int start, int end) {
        if (list.isEmpty() || start>list.get(list.size()-1)[1]){
            list.add(new int[]{start,end});
            return true;
        }
        int l=0,r=list.size()-1,ans=-1;
        while (l<=r) {
            int mid=(l+r)/2;
            if (start<=list.get(mid)[1]) {
                ans=mid;
                r=mid-1;
            } else {
                l=mid+1;
            }
        }
        if (start==list.get(ans)[1]){
            if (ans==list.size()-1 || end<list.get(ans+1)[0]){
                list.get(ans)[1]=end;
                return true;
            }else if (end==list.get(ans+1)[0]){
                list.get(ans)[1]=list.get(ans+1)[1];
                list.remove(ans+1);
                return true;
            }
        }
        if (end==list.get(ans)[0]){
            list.get(ans)[0]=start;
            return true;
        }
        if (end<list.get(ans)[0]){
            list.add(ans,new int[]{start,end});
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        Solution729 solution729=new Solution729();
        System.out.println(solution729.book(10,20));
        System.out.println(solution729.book(15, 25));
        System.out.println(solution729.book(20, 30));
    }
}
